package com.by.service.Impl;


import com.by.bean.Employess;

import com.by.bean.User;
import com.by.bean.UserType;
import com.by.common.bean.PageBean;
import com.by.common.crud.BaseServiceImpl;
import com.by.common.utils.Assert;
import com.by.mapper.EmployessMapper;
import com.by.mapper.UserMapper;
import com.by.query.EmployessQuery;
import com.by.service.EmployessService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class  EmployessServiceImpl extends BaseServiceImpl<Employess, EmployessMapper> implements EmployessService {


  @Autowired
  UserMapper userMapper;

  @Transactional
  @Override
  public int save(Employess employess) {
    //保存用户
    User user = new User(employess.getAccount(), employess.getPassword(), UserType.EMPLOYESS_USER);
    int rs = userMapper.save(user);
    Assert.isZero(rs,"用户信息保存失败");

    employess.setEmployee_id(user.getId());

    return super.save(employess);
  }

  @Override
  public PageBean query(EmployessQuery query) {
    //分页查询
    PageHelper.startPage(query.getPage(), query.getPageSize());//后面紧跟查询
    List list = baseMapper.query(query);
    PageInfo info = new PageInfo(list);
    return new PageBean(query.getPage(), query.getPageSize(), info.getTotal(), list);
  }


}
